<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	<script type="text/javascript">
		var cal=function(x){
			var num1=document.getElementById("num1").value;
			var num2=document.getElementById("num2").value;
			if(isNaN(num1)||isNaN(num2)){
				alert("数字不合法");
				return;
			}
			var res=0;
			switch(x){//x确认运算符号
//未完成部分：输入不是数字，输入分母为0，没有输入的值，0.1+0.2=0.3000000000004
				case 1:
				res=(num1-0)+(num2-0);//字符串转数字
				document.getElementById("res").value=res;
				break;
				case 2:
				res=(num1-0)-(num2-0);
				document.getElementById("res").value=res;
				break;
				case 3:
				res=(num1-0)*(num2-0);
				document.getElementById("res").value=res;
				break;
				case 4:
				if(num2!=0){
				res=(num1-0)/(num2-0);
				document.getElementById("res").value=res;
				break;
				}
				else{
					alert("分母不为0");
				}
			}
		}
	</script>
	</head>
	<body>
	
			<div>第一个数<input type="text" name="" id="num1" value="" /></div><br>
			<div>第二个数<input type="text" name="" id="num2" value="" /></div><br>
		
		<button onclick="cal(1)" id="btn1">+</button>
		<button onclick="cal(2)" class="btn2">-</button>
		<button onclick="cal(3)" name="btn3">*</button>
		<button onclick="cal(4)">/</button><br>
		
		<div>计算结果<input type="text" name="" id="res" value="" /></div><br>
		<script type="text/javascript">
			
			var btn1=document.getElementById("btn1");
			console.log(btn1);
			var btn2=document.getElementsByClassName("btn2")[0];//数组
			console.log(btn2);
			var btn3=document.getElementsByName("btn3")[0];//数组
			console.log(btn3);
			var btn4=document.querySelector(".btn2+button+button");//相邻兄弟
			console.log(btn4);
		
			
			
			
		</script>
	</body>
</html>
